package com.czollmann.android.views;

import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ImageView.ScaleType;

import com.czollmann.android.DateObjectWrapper;
import com.czollmann.android.R;
import com.czollmann.android.StaticMethods;
import com.czollmann.android.utils.DateObjectWrapperUtils;
import com.czollmann.android.utils.LogUtils;

public class TimelineView extends LinearLayout {
  int viewHeight = 100;
  int viewWidth = 480;
  ImageView lastImageView = null;
  boolean drawn = false;

  public TimelineView(Context context) {
    super(context);
  }

  public TimelineView(Context context, AttributeSet attributeSet) {
    super(context, attributeSet);
    StaticMethods.setTimelineView(this);
    setOrientation(LinearLayout.HORIZONTAL);
    setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    if (isInEditMode()) {
      DateObjectWrapperUtils.createTestData();
    }
    setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN
                  || event.getAction() == MotionEvent.ACTION_UP
                  || event.getAction() == MotionEvent.ACTION_MOVE) {
          int x = (int) event.getX();
          double featureWidth = (double) viewWidth
                    / DateObjectWrapperUtils.getDateObjectWrappers().size();
          // LogUtils.debuglog("-----");
          // LogUtils.debuglog("maxWidth: " + maxWidth);
          // LogUtils.debuglog("DateObjectWrapperUtils.getDateObjectWrappers().size(): "
          // + DateObjectWrapperUtils.getDateObjectWrappers().size());
          // LogUtils.debuglog("featureWidth: " + featureWidth);
          int activeFeature = (int) ((x + (featureWidth / 2)) / featureWidth);
          // LogUtils.debuglog("timeline click on page: " + activeFeature);
          HomeView homeView = StaticMethods.getHomeView();
          if (homeView != null) {
            homeView.doScroll(activeFeature);
          }
          return true;
        } else {
          return false;
        }
      }
    });
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    draw();
  }

  private void draw() {
    if (!drawn) {
      drawn = true;
      LogUtils.infolog("TimelineView: draw()");
      if (getMeasuredHeight() != 0) {
        viewHeight = getMeasuredHeight();
      }
      if (DateObjectWrapperUtils.getDateObjectWrappers().size() > 0) {
        int maxObjectWrapperCount = DateObjectWrapperUtils.getMaxObjectWrapperCount();
        List<DateObjectWrapper> dateObjectWrappers = DateObjectWrapperUtils.getDateObjectWrappers();
        int width = 10;
        if (getMeasuredWidth() != 0) {
          viewWidth = getMeasuredWidth();
        }
        if (viewWidth != 0) {
          width = viewWidth / dateObjectWrappers.size();
        }
        for (int i = 0; i < dateObjectWrappers.size(); i++) {
          DateObjectWrapper dateObjectWrapper = dateObjectWrappers.get(i);
          int height = calculateHeight(maxObjectWrapperCount,
                                       dateObjectWrapper.getObjects().size());
          ImageView imageView = new ImageView(getContext());
          imageView.setImageResource(R.drawable.px_orange);
          imageView.setLayoutParams(new LayoutParams(width, height, Gravity.BOTTOM));
          imageView.setScaleType(ScaleType.FIT_XY);
          imageView.setTag(dateObjectWrapper);
          if (i % 2 == 0) {
            imageView.setAlpha(200);
          }
          addView(imageView);
        }
      }
    }
  }

  private int calculateHeight(int peak, int size) {
    return (viewHeight / peak) * size;
  }

  public void setActiveFeature(int activeFeature) {
    ImageView imageView = (ImageView) this.getChildAt(activeFeature);
    if (imageView != null) {
      if (lastImageView != null) {
        lastImageView.setImageResource(R.drawable.px_orange);
      }
      lastImageView = imageView;
      imageView.setImageResource(R.drawable.px_blue);
    }
  }
}
